<?php
class SysAlbumBusi {
	//插入一条相册记录
	public function addAlbum($condition){		
	    //插入的标题名不能为空
	    if(!isset($condition['albumTitle']))
	        return "相册名不能为空！";
	    //查看相册名是否已存在
	    $isExist = $this->albumIsExist($condition['albumTitle']);
	    if($isExist)
	        return "该相册名的相册已存在!";
	    
	    global $mysqlTools;
		//生成唯一ID
		$uniqid = getCurrentId();
		$userId = $_SESSION['userInfo']['userId'];
		
		//参数处理注入SQL安全问题
		$condition = $mysqlTools->check_input($condition);	
		try {
		    $mysqlTools->startTransaction();
		    
		    $result = $mysqlTools->executeSql("
		            insert into sys_album(albumId,albumTitle,albumCreateTime,userId) values($uniqid,$condition[albumTitle],now(),$userId)
		            ");
		    
		    $mysqlTools->commitTransaction();
		    
		} catch (\Exception $e) {
		    $mysqlTools->rollback();
		    
		    return "数据库插入发生异常";
		}
		if($result)
		    return $uniqid;
	    else 
	        return $result;
	}
	
	//删除一个相册
	public function delAlbum($condition){
	    global $mysqlTools;
	    session_start();
	    $userId = $_SESSION['userInfo']['userId'];
	
	    //参数处理注入SQL安全问题
	    $condition = $mysqlTools->check_input($condition);
	    try {
	        $mysqlTools->startTransaction();
	
	        $result = $mysqlTools->executeSql("
                delete from sys_album where albumId = $condition[albumId] and userId = 1000000000000154;
            ");

            $mysqlTools->commitTransaction();
            
            if(!$userId){
                return '请先登录后再进行删除操作！';
            }
    	} catch (\Exception $e) {
        	$mysqlTools->rollback();
        	
        	return "数据库插入发生异常";
    	}
	    return $result;
	}
	
	/**
	 * 相册名是否已存在
	 * @param string $albumTitle 标题
	 * @return boolean
	 */
	public function albumIsExist($albumTitle){
	    global $mysqlTools;
	    session_start();
	    //参数处理注入SQL安全问题
	    $albumTitle = $mysqlTools->check_input($albumTitle);
	    
	    //用户ID
	    $userId = $_SESSION["userInfo"]['userId'];
	    
	    //查询该用户的该相册名是否已存在
	    $albumTitleCount = $mysqlTools->selectSql("
	            select count(*) as count from sys_album where albumTitle = $albumTitle and userId = $userId
	            ")[0]['count'];
	    
	    
	    if(intval($albumTitleCount)>0)
	        return true;
	    else
	        return false;
	}
	
	//根据用户ID查询用户相册信息
	public function queryAlbumByUserId($userId){
		global $mysqlTools;
		
		$userId = $mysqlTools->check_input($userId);
		
		//查询用户创建时间
		$createDateTime = $mysqlTools->selectSql("
		    SELECT
             su.userCreateTime
            from 
            	sys_user su 
            where 
            	su.userId = $userId        
		")[0]['userCreateTime'];
		
		$defaultAlbums = array("albumId"=>"0","albumTitle"=>"默认相册","albumCreateTime"=>$createDateTime);
		
		$moreAlbums = $mysqlTools->selectSql("
		    SELECT
            	sa.albumId,
            	sa.albumTitle,
            	sa.albumCreateTime,
            	sa.userId
            FROM
            	sys_album sa
		    WHERE
		        sa.userId = $userId
		");
		
		if(is_array($moreAlbums) && count($moreAlbums)>0){
		    array_push($moreAlbums,$defaultAlbums);
		    $albums = $moreAlbums;
		}
		else 
		  $albums = $defaultAlbums;
		
		return $albums;
	}
	
	//删除一个用户
	public function delSysUser($albumId){
	    global $mysqlTools;
	    //生成唯一ID
	    $uniqid = getCurrentId();
	    
	    //参数处理注入SQL安全问题
	    $albumId = $mysqlTools->check_input($albumId);
	    
	    try {
	        $mysqlTools->startTransaction();
	        $result = $mysqlTools->executeSql("
	                 delete from sys_album where albumId = $albumId;
	                ");
	        $mysqlTools->commitTransaction();
	        
	    } catch (\Exception $e) {
	        $mysqlTools->rollback();
	        
	        return "数据库插入发生异常";
	    }
	    return $result;
	}
	
	//通过相册ID查询相册名
    public function queryAlbumByAlbumId($albumId){
		global $mysqlTools;
		
		$albumId = $mysqlTools->check_input($albumId);
		
		//查询用户相册信息
		$album = $mysqlTools->selectSql("
		    SELECT
            	sa.albumId,
            	sa.albumTitle,
            	sa.albumCreateTime,
            	sa.userId
            FROM
            	sys_album sa
            WHERE
            	sa.albumId = $albumId
		")[0];
		
		return $album;
	}
	
	//修改一个用户
	public function updateSysUser(){
		
	}
}	

?>